﻿Imports System.Data.SqlClient

Public Class 财务台账
    Private connectionString As String = DbConnectionHelper.GetConnectString

    Private Sub 财务台账_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 初始化日期和下拉框
        开始日期_dtp.Value = Date.Today.AddMonths(-1)
        结束日期_dtp.Value = Date.Today

        类型_cmb.Items.Clear()
        类型_cmb.Items.Add("全部")
        类型_cmb.Items.Add("入账")
        类型_cmb.Items.Add("支出")
        类型_cmb.Items.Add("盘点")
        类型_cmb.Items.Add("期初")
        类型_cmb.SelectedIndex = 0

        更新统计数据()
    End Sub

    Private Sub 查询btn_Click(sender As Object, e As EventArgs) Handles 查询btn.Click
        查询台账()
    End Sub
    Private Sub 更新统计数据()
        Try
            Using conn As New SqlConnection(connectionString)
                conn.Open()

                Using cmd As New SqlCommand("usp_GetTZTongji", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    Dim da As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    da.Fill(dt)

                    '==============================
                    ' 初始化数值
                    '==============================
                    Dim 日收入 As Decimal = 0, 周收入 As Decimal = 0, 月收入 As Decimal = 0
                    Dim 日支出 As Decimal = 0, 周支出 As Decimal = 0, 月支出 As Decimal = 0
                    Dim 日盘点 As Decimal = 0, 周盘点 As Decimal = 0, 月盘点 As Decimal = 0

                    '==============================
                    ' 遍历数据分类汇总
                    '==============================
                    For Each row As DataRow In dt.Rows
                        Dim 区间 As String = row("时间区间").ToString()
                        Dim 类型 As Integer = Convert.ToInt32(row("JYLX"))
                        Dim 金额 As Decimal = Convert.ToDecimal(row("金额合计"))

                        Select Case 区间
                            Case "日"
                                Select Case 类型
                                    Case 1 : 日收入 += 金额
                                    Case 2 : 日支出 += 金额
                                    Case 3 : 日盘点 += 金额
                                End Select
                            Case "周"
                                Select Case 类型
                                    Case 1 : 周收入 += 金额
                                    Case 2 : 周支出 += 金额
                                    Case 3 : 周盘点 += 金额
                                End Select
                            Case "月"
                                Select Case 类型
                                    Case 1 : 月收入 += 金额
                                    Case 2 : 月支出 += 金额
                                    Case 3 : 月盘点 += 金额
                                End Select
                        End Select
                    Next

                    '==============================
                    ' 更新界面 Label
                    '==============================
                    本日收入_lbl.Text = "本日收入：" & 日收入.ToString("N2")
                    本周收入_lbl.Text = "本周收入：" & 周收入.ToString("N2")
                    本月收入_lbl.Text = "本月收入：" & 月收入.ToString("N2")

                    本日支出_lbl.Text = "本日支出：" & 日支出.ToString("N2")
                    本周支出_lbl.Text = "本周支出：" & 周支出.ToString("N2")
                    本月支出_lbl.Text = "本月支出：" & 月支出.ToString("N2")

                    本日盘点_lbl.Text = "本日盘点：" & 日盘点.ToString("N2")
                    本周盘点_lbl.Text = "本周盘点：" & 周盘点.ToString("N2")
                    本月盘点_lbl.Text = "本月盘点：" & 月盘点.ToString("N2")
                End Using
            End Using

        Catch ex As Exception
            LogHelper.WriteLog("更新统计数据出错：" & ex.Message)
        End Try
    End Sub

    Private Sub 查询台账()
        Try
            Using conn As New SqlConnection(connectionString)
                Using cmd As New SqlCommand("usp_GetTZJL", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    ' 时间参数
                    cmd.Parameters.AddWithValue("@start", 开始日期_dtp.Value.Date)
                    cmd.Parameters.AddWithValue("@end", 结束日期_dtp.Value.Date.AddDays(1).AddSeconds(-1))

                    ' 类型转换为数字
                    Dim selectedTypeText As String = 类型_cmb.SelectedItem.ToString()
                    Dim selectedType As Integer

                    Select Case selectedTypeText
                        Case "全部"
                            selectedType = 0
                        Case "入账"
                            selectedType = 1
                        Case "支出"
                            selectedType = 2
                        Case "盘点"
                            selectedType = 3
                        Case "期初"
                            selectedType = 4
                        Case Else
                            selectedType = 0
                    End Select

                    ' 记录日志
                    LogHelper.WriteLog($"查询财务台账：exec usp_GetTZJL '{开始日期_dtp.Value:yyyy-MM-dd HH:mm:ss}','{结束日期_dtp.Value.AddDays(1).AddSeconds(-1):yyyy-MM-dd HH:mm:ss}',{selectedType}")

                    ' 传入参数
                    cmd.Parameters.AddWithValue("@type", selectedType)

                    ' 查询并绑定
                    Dim da As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    da.Fill(dt)
                    台账表_dgv.DataSource = dt
                End Using
            End Using

        Catch ex As Exception
            LogHelper.WriteLog("查询出错：" & ex.Message)
            MessageBox.Show("查询出错：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


End Class
